#include "framecore.h"
#include <qmath.h>

QQtDictionary gLightDict;
QQtDictionary gLightGroupDict;
QQtDictionary gLightSceneDict;
//这两个是经常操作的
QQtDictionary gUsingLightsDict;
QQtDictionary gSearchingLightsDict;

FrameCore::FrameCore ( QWidget* parent ) : QWidget ( parent )
{

}


QRect calculatePosition ( QWidget* parent, int index, int bradius, int lradius, int lcount, int shift  )
{
    int width = parent->width();
    int height = parent->height();

    /*小圆的个数*/
    //int lcount = 10;

    /*小圆半径*/
    //int lradius = 40;

    /*大圆半径*/
    int radius = bradius;//width / 2 - lradius - 10;
    //这里可以减去lradius,实现直接贴内壁.现在不减去,是在沿上.

    /*小圆的偏移弧度，不是角度哦，标准C正弦计算用弧度0-2π*/
    /*这里必须用qreal，用int进行计算，得不出正确边长*/
    qreal radian = ( index * ( 360 / lcount ) + shift ) / 180.0 * M_PI;

    /*小圆的左上角坐标*/
    int x = width / 2 + radius * qSin ( radian ) - lradius;
    int y = height / 2 - radius * qCos ( radian ) - lradius;

    return QRect ( x, y, lradius * 2, lradius * 2 );
}

void initLightsDBtoDict()
{
    initLightDict();
    initGroupDict();
    initSceneDict();
    //error
    initUsingDict();
    initSearchingDict();
}

QString findIconNameBySceneName ( QString sceneName )
{
    QQtDictionaryListIterator itor ( gLightSceneDict.getList() );
    QString icon = "";

    while ( itor.hasNext() )
    {
        const QQtDictionary& node = itor.next();
        QString sceN = node["name"].getValue().toString();

        if ( sceN == sceneName )
        {
            icon = node["icon"].getValue().toString();
            break;
        }
    }

    return icon;
}

//1
void initLightDict()
{
    QQtSqlDatabaseConnection db;
    db = newDatabaseConnection();
    QString dbName = "Lights.db";
    //pline() << dbName;
    useDatabase ( db,  dbName );

    QQtSqlQuery query ( db );
    //query.setDebug ( true );

    //保存全部灯。999个 连续
    query.exec ( "select * from light" );

    while ( query.next() )
    {
        //id 就是lightNo
        int lightNo = query.record().value ( "ID" ).toInt();
        int deley = query.record().value ( "deley" ).toInt();
        int loop = query.record().value ( "loop" ).toInt();
        QString scene = query.record().value ( "scene" ).toString();
        gLightDict[lightNo]["deley"] = deley;
        gLightDict[lightNo]["loop"] = loop;
        gLightDict[lightNo]["scene"] = scene;
    }

    query.finish();
    closeDatabase ( db );
}

//3
void initSceneDict()
{
    QQtSqlDatabaseConnection db;
    db = newDatabaseConnection();
    QString dbName = "Lights.db";
    //pline() << dbName;
    useDatabase ( db,  dbName );

    QQtSqlQuery query ( db );
    //query.setDebug ( true );

    //这个是内置的，48个
    //保存全部scene。
    query.exec ( "select * from lightScene" );

    while ( query.next() )
    {
        //id scene id
        int sceneId = query.record().value ( "ID" ).toInt();
        QString name = query.record().value ( "name" ).toString();
        int times = query.record().value ( "timesMinite" ).toInt();
        int lightTime = query.record().value ( "lightTime" ).toInt();
        QString icon = query.record().value ( "icon" ).toString();
        gLightSceneDict[sceneId]["name"] = name;
        gLightSceneDict[sceneId]["times"] = times;
        gLightSceneDict[sceneId]["lightTime"] = lightTime;
        gLightSceneDict[sceneId]["icon"] = icon;
    }

    query.finish();
    closeDatabase ( db );
}

//2
void initGroupDict()
{
    QQtSqlDatabaseConnection db;
    db = newDatabaseConnection();
    QString dbName = "Lights.db";
    //pline() << dbName;
    useDatabase ( db,  dbName );

    QQtSqlQuery query ( db );
    //query.setDebug ( true );

    //这个是内置的，20个
    //保存全部灯组。
    query.exec ( "select * from lightGroup" );

    while ( query.next() )
    {
        //id group id
        int groupId = query.record().value ( "ID" ).toInt();
        QString name = query.record().value ( "name" ).toString();
        QString lights = query.record().value ( "lights" ).toString();
        gLightGroupDict[groupId]["name"] = name;
        gLightGroupDict[groupId]["lights"] = lights;
        //把lights保存为列表。
        QStringList lightsList = lights.split ( ',' );
        gLightGroupDict[groupId]["list"] = lightsList;
    }

    query.finish();
    closeDatabase ( db );
}

//4
void initUsingDict()
{
    QQtSqlDatabaseConnection db;
    db = newDatabaseConnection();
    QString dbName = "Lights.db";
    //pline() << dbName;
    useDatabase ( db,  dbName );

    QQtSqlQuery query ( db );
    //query.setDebug ( true );

    //初始化保存着的那些使用里的灯
    query.exec ( "select * from lightGroupUsing" );

    while ( query.next() )
    {
        /*
         * 数据结构
         * a list (name=scene single-light, name=group multi-light)
         * <index> ["name"], ["lightNo."], ["scene"] (a map - light info)
         * <index> (a list - group light)
         *          <index> ["name"], ["lightNo."], ["scene"] (a map - light info)
         *          <index> ...
         * <index> ...
         *
         * 代码样式
         * 灯
         * btn[index]["name"] 这是灯或组的名字
         * btn[index]["type"] 是这个节点的类型 [灯、组]
         * btn[index]["lightNo." "scene" "button" "type" ... "name"] 灯信息 一般会忽略name
         * 组
         * btn[index]["name"] 是这个节点的名字 就是组名
         * btn[index]["type"] 是这个节点的类型 [灯、组]
         * btn[index][index1]["lightNo." "scene" "button" "type" ... "name"]
         */
        //id是这个button的index。button的名称有时候有用，有时候没用。
        int btnIndex = query.record().value ( "ID" ).toInt();
        QString name = query.record().value ( "name" ).toString();
        QString lightNo = query.record().value ( "lightNo." ).toString();
        QStringList lightNoList = lightNo.split ( ',' );

        if ( lightNoList.size() < 1 )   //empty 里出错了。
        {
            pline() << "这个lightGroupUsing灯出错了" << btnIndex;
            continue;
        }

        if ( lightNoList.size() > 1 )   //group
        {
            /*这里很特殊，这里设置了group名，QQtDictionary内部类型会跟随变化。这里是个，具名节点。*/
            gUsingLightsDict[btnIndex]["type"] = "group";
            gUsingLightsDict[btnIndex]["name"] = name;
            gUsingLightsDict[btnIndex]["lightNo."] = lightNo;

            int size = lightNoList.size();

            for ( int lightInGroupBtnIndex = 0; lightInGroupBtnIndex < size ; lightInGroupBtnIndex++ )
            {

                int lightNoInGroup = lightNoList[lightInGroupBtnIndex].trimmed().toInt();
                QString lightNoStringInGroup = lightNoList[lightInGroupBtnIndex].trimmed();
                gUsingLightsDict[btnIndex][lightInGroupBtnIndex]["type"] = "light"; //这里肯定是个灯
                gUsingLightsDict[btnIndex][lightInGroupBtnIndex]["name"] = gLightDict[lightNoInGroup]["scene"];//ignore
                gUsingLightsDict[btnIndex][lightInGroupBtnIndex]["lightNo."] = lightNoStringInGroup;
                gUsingLightsDict[btnIndex][lightInGroupBtnIndex]["scene"] = gLightDict[lightNoInGroup]["scene"] ;
            }

            continue;
        }

        //单个灯的name始终是没有用的。
        int lightNoInt = lightNo.trimmed().toInt();
        gUsingLightsDict[btnIndex]["type"] = "light";
        gUsingLightsDict[btnIndex]["name"] = gLightDict[lightNoInt]["scene"];//ignore
        gUsingLightsDict[btnIndex]["lightNo."] = lightNo;
        gUsingLightsDict[btnIndex]["scene"] = gLightDict[lightNoInt]["scene"];
    }

    query.finish();
    closeDatabase ( db );
}

//5
void initSearchingDict()
{
    QQtSqlDatabaseConnection db;
    db = newDatabaseConnection();
    QString dbName = "Lights.db";
    //pline() << dbName;
    useDatabase ( db,  dbName );

    QQtSqlQuery query ( db );
    //query.setDebug ( true );

    //初始化过去搜索到的灯
    query.exec ( "select * from lightSearching" );

    while ( query.next() )
    {
        //id
        int id = query.record().value ( "ID" ).toInt();
        int type = query.record().value ( "type" ).toInt();

        gSearchingLightsDict[id]["type"] = type;
    }


    query.finish();
    closeDatabase ( db );
}
